using System.Collections.Generic;
using System.Linq;
using WatiN.Core;

namespace YatFram.Browser.Watin
{
    public class WatinRadioBox : ISingleOption
    {
        private readonly IEnumerable<RadioButton> radioOptions;

        public WatinRadioBox(IEnumerable<RadioButton> radioOptions)
        {
            this.radioOptions = radioOptions;
        }

        public string SelectedValue
        {
            get
            {
                var selectedOption = radioOptions.SingleOrDefault(option => option.Checked);
                return selectedOption == null ? null : selectedOption.GetAttributeValue("value");
            }
        }

        public void Select(string value)
        {
            radioOptions.Single(option => option.GetAttributeValue("value") == value).Checked = true;
        }
    }
}